// generate-vite-config.js
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 组件目录
const componentsDir = path.resolve(__dirname, 'src/components');

// 获取所有组件目录
const componentNames = fs.readdirSync(componentsDir).filter(file => {
  return fs.statSync(path.join(componentsDir, file)).isDirectory();
});

// 构建导入和导出语句
let importStatements = '';
let exportStatements = '';
let exportDefaultObject = '{';

componentNames.forEach(name => {
  const componentPath = `./components/${name}/runtime/${name}.vue`;
  const componentName = name;

  importStatements += `import ${componentName} from '${componentPath}';\n`;
  exportStatements += `export { ${componentName} };\n`;
  exportDefaultObject += `\n  ${componentName},`;
});

exportDefaultObject = exportDefaultObject.slice(0, -1) + '\n};';

// 构建 index.js 内容
const indexContent = `${importStatements}\n\n${exportStatements}\n\nexport default ${exportDefaultObject}`;

// 写入 index.js 文件
fs.writeFileSync(path.resolve(__dirname, 'src/index.js'), indexContent, 'utf-8');

console.log('组件已动态写入 index.js 文件。');